package executor.dead;

import java.util.concurrent.*;

/**
 * 除了线程池大小限制之外，还可能由于其他资源上的约束而存在一些隐式的限制
 */
public class ThreadDeadLock {
    ExecutorService exec = Executors.newSingleThreadExecutor();
    public final TwoPrintTask twoPrintTask = new TwoPrintTask();

    //task 必须与 ExecutorService 联合使用
    public class TwoPrintTask implements Callable<String> {
        public String call() throws Exception {
            Future<String> print1Result, print2Result;
            print1Result = exec.submit(new PrintTaskCallable("print1 task"));
            print2Result = exec.submit(new PrintTaskCallable("print2 task"));
			//TwoPrintTask中等待同一个Executor下其他任务的执行结果，Executor只有一个线程
			//饥饿死锁
            return print1Result.get() + print2Result.get() + "success";
        }
    }

    public static void main(String[] args) {
        ThreadDeadLock threadDeadLock = new ThreadDeadLock();
        Future<String> twoPrintTaskResult = threadDeadLock.exec.submit(threadDeadLock.twoPrintTask);
        try {
            System.out.println(twoPrintTaskResult.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
